//
//  counter.m
//  WashForU
//
//  Created by 王俊 on 15/12/1.
//  Copyright © 2015年 王俊. All rights reserved.
//

#import "Counter.h"

@implementation Counter

-(instancetype)initView
{
    self = [super init];
    self.minusBtn = [UIButton new];
    self.plusBtn = [UIButton new];
    self.numLabel = [UILabel new];
    
    [self.minusBtn setImage:[UIImage imageNamed:@"minus-1"] forState:UIControlStateNormal];
    [self.minusBtn setImage:[UIImage imageNamed:@"minus-2"] forState:UIControlStateHighlighted];
    [self.minusBtn addTarget:self action:@selector(minusBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.plusBtn setImage:[UIImage imageNamed:@"plus-1"] forState:UIControlStateNormal];
    [self.plusBtn setImage:[UIImage imageNamed:@"plus-2"] forState:UIControlStateHighlighted];
    [self.plusBtn addTarget:self action:@selector(plusBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    if (self.numb == 0) {
        self.numb = 1;
    }
    self.numLabel.text = [NSString stringWithFormat:@"%d",self.numb];
    self.numLabel.textColor = [UIColor CustomOrangeColor];
    self.numLabel.textAlignment = NSTextAlignmentCenter;
    self.numLabel.layer.borderWidth = 1;
    self.numLabel.layer.borderColor = [UIColor GrayColor_188].CGColor;
    self.numLabel.backgroundColor = [UIColor GrayColor_237];
    [self addSubview:self.minusBtn];
    [self addSubview:self.plusBtn];
    [self addSubview:self.numLabel];
    
    [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.top.bottom.equalTo(self);
        make.width.equalTo(self).multipliedBy(1.0/3.0);
    }];
    [self.minusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.top.bottom.equalTo(self.numLabel);
        make.trailing.equalTo(self.numLabel.mas_leading).offset(1);
    }];
    [self.plusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.top.bottom.equalTo(self.numLabel);
        make.leading.equalTo(self.numLabel.mas_trailing).offset(-1);
    }];
    return self;
    
}
-(void)minusBtnClicked
{

    self.numb > 0 ? (self.numb --) : (self.numb = 0);
    self.numLabel.text = [NSString stringWithFormat:@"%d",self.numb];

    
}
-(void)plusBtnClicked
{
    self.numb ++;
    self.numLabel.text = [NSString stringWithFormat:@"%d",self.numb];
 
}
@end
